package com.bwie.service.impl;

import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.ProdInfoMapper;
import com.bwie.mapper.TypeMapper;
import com.bwie.pojo.Prod;
import com.bwie.pojo.ProdInfo;
import com.bwie.pojo.Type;
import com.bwie.service.ProdService;
import com.bwie.mapper.ProdMapper;
import com.bwie.utils.R;
import com.bwie.vo.ProdInfoVo;
import com.bwie.vo.ProdVo;
import com.bwie.vo.TypeVo;
import com.github.tobato.fastdfs.FdfsClientConfig;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import io.jsonwebtoken.Jwts;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author 陶仕豪
 * @description 针对表【tb_prod(商品表)】的数据库操作Service实现
 * @createDate 2025-07-19 09:43:21
 */
@Service
public class ProdServiceImpl extends ServiceImpl<ProdMapper, Prod>
        implements ProdService {

    @Autowired
    ProdMapper prodMapper;
    @Autowired
    TypeMapper typeMapper;
    @Autowired
    FastFileStorageClient fastFileStorageClient;

    @Autowired
    HttpServletRequest request;
    @Autowired
    ProdInfoMapper prodInfoMapper;


    @Override
    public R getProdList(ProdVo propVo) {
        Page<Prod> prodPage = new Page<>(propVo.getPageNum(), propVo.getPageSize());
        Page<Prod> page = prodMapper.getProdList(prodPage, propVo);
        return R.OK(page);
    }

    @Override
    public R getTypeList(TypeVo typeVo) {
        QueryWrapper<Type> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(typeVo.getTypeName())) {
            wrapper.lambda().like(Type::getTypeName, typeVo.getTypeName());
        }
        if (typeVo.getTypeStatus() != null) {
            wrapper.lambda().eq(Type::getTypeStatus, typeVo.getTypeStatus());
        }
        List<Type> typeList = typeMapper.selectList(wrapper);
        List<Tree<String>> treeList = TreeUtil.build(typeList, "0", (tree, type) -> {
            type.setName(tree.getTypeName());
            type.setId(tree.getTypeId()+"");
            type.setParentId(tree.getTypePid()+"");
            type.putExtra("typeId",tree.getTypeId());
            type.putExtra("typeName",tree.getTypeName());
            type.putExtra("typeImage",tree.getTypeImage());
            type.putExtra("typeTop",tree.getTypeTop());
            type.putExtra("typeStatus",tree.getTypeStatus());
        });
        return R.OK(treeList);
    }

    @Override
    public R uploadFile(MultipartFile file) {
        try {
            String extName = FileNameUtil.extName(file.getOriginalFilename());
            StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), extName, null);
            String url="http://192.168.80.192:8888/"+storePath.getFullPath();
            return R.OK(url);
        } catch (Exception e) {
            e.printStackTrace();
            return R.ERROR(500,"上传失败");
        }
    }

    @Override
    public R addType(Type tbType) {
        String token = request.getHeader("token");
        if(token==null){
            return R.ERROR(500,"请先登录");
        }
        String userName = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody().get("userName", String.class);
        Type type = typeMapper.selectById(tbType.getTypeId());
        if(type!=null){
            return R.ERROR(500,"分类名称已存在");
        }
        tbType.setCreateBy(userName);
        tbType.setCreateTime(new Date());
        typeMapper.insert(tbType);
        return R.OK();
    }

    @Override
    public R deleteAll(List<Prod> prodList) {
        if(prodList==null){
            return R.ERROR(500,"请选择需要删除的数据");
        }
        for (Prod prod : prodList) {
            prodMapper.deleteById(prod.getProdId());
        }
        return R.OK();
    }

    @Override
    public R getTypeAll() {
        List<Type> typeList = typeMapper.selectList(null);
        List<Tree<String>> treeList = TreeUtil.build(typeList, "0", (tree, type) -> {

            type.setName(tree.getTypeName());
            type.setId(tree.getTypeId()+"");
            type.setParentId(tree.getTypePid()+"");
            type.putExtra("label",tree.getTypeName());
            type.putExtra("value",tree.getTypeId());
            type.putExtra("typeImage",tree.getTypeImage());
            type.putExtra("typeTop",tree.getTypeTop());
            type.putExtra("typeStatus",tree.getTypeStatus());
        });
        return R.OK(treeList);
    }

    @Override
    public R addProd(Prod prod) {
        Prod prod1 = prodMapper.selectOne(new QueryWrapper<Prod>().lambda().eq(Prod::getProdSpu, prod.getProdSpu()));
        if(prod1!=null){
            return R.ERROR(500,"SPU不能重复");
        }
        String token = request.getHeader("token");
        if(token==null){
            return R.ERROR(500,"请先登录");
        }
        String userName = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody().get("userName", String.class);
        prod.setCreateBy(userName);
        prod.setProdStatus(0);
        prod.setCreateTime(new Date());
        prodMapper.insert(prod);
        return R.OK();
    }

    @Override
    public R getProdInfoList() {
        List<ProdInfo> prodInfoList = prodInfoMapper.selectList(null);
        return R.OK(prodInfoList);
    }

    @Override
    public R getProdById(Integer prodId) {
        //获取商品信息
        Prod prod = prodMapper.selectById(prodId);
        if(prod==null){
            return R.ERROR(500,"商品不存在");
        }
        return R.OK(prod);
    }

    @Override
    public R getProdInfoListAll(ProdInfoVo prodInfoVo) {
        //构造分页参数
        Page<ProdInfo> prodInfoPage = new Page<>(prodInfoVo.getPageNum(), prodInfoVo.getPageSize());
        QueryWrapper<ProdInfo> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(ProdInfo::getProdId,prodInfoVo.getProdId()).orderByAsc(ProdInfo::getInfoId);
        Page<ProdInfo> page = prodInfoMapper.selectPage(prodInfoPage, wrapper);

        return R.OK(page);
    }

    @Override
    public R addProdInfo(ProdInfo tbProdInfo) {
        String token = request.getHeader("token");
        if(token==null){
            return R.ERROR(500,"请先登录");
        }
        String userName = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody().get("userName", String.class);
        Prod prod = prodMapper.selectById(tbProdInfo.getProdId());
        if(prod==null){
            return R.ERROR(500,"商品不存在");
        }
        //判断商品SKU是否存在
        QueryWrapper<ProdInfo> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(ProdInfo::getProdId,tbProdInfo.getProdId())
                        .eq(ProdInfo::getProdName,tbProdInfo.getProdName());
        ProdInfo one = prodInfoMapper.selectOne(wrapper);
        if(one!=null){
            return R.ERROR(500,"商品SKU不存在");
        }

        tbProdInfo.setCreateTime(new Date());
        tbProdInfo.setCreateBy(userName);
        prodInfoMapper.insert(tbProdInfo);
        return R.OK();
    }


}




